package samsung.sds.roleManagement.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import samsung.sds.resourceManagement.model.ResourceToRole;
import samsung.sds.resourceManagement.service.ResourceManagementService;
import samsung.sds.roleManagement.dao.RoleManagementMapper;
import samsung.sds.roleManagement.model.RoleManagementEntity;
import samsung.sds.roleManagement.model.RoleToResEntity;
import samsung.sds.roleManagement.model.UserToRoleEntity;
import samsung.sds.roleManagement.service.RoleManagementService;
import samsung.sds.system.common.baseQuery.AbstractBaseService;
import samsung.sds.system.common.baseQuery.BaseMapper;
import samsung.sds.system.common.baseQuery.model.PageData;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.system.utils.CommonTools;
import samsung.sds.system.utils.Constants;
import samsung.sds.userManagement.model.UserEntity;

/**
 * 
 * @author fei3.liu
 * @updateDate 2016-01-26 roleManagement的service组件
 */
@Service
public class RoleManagementServiceImpl extends AbstractBaseService<RoleManagementEntity> implements RoleManagementService {

	@Autowired
	private RoleManagementMapper roleManagementMapper;

	@Autowired
	private ResourceManagementService resourceManagementServiceImpl;

	protected BaseMapper<RoleManagementEntity> getMapper() {
		return roleManagementMapper;
	}

	/**
	 * Get table of SYS_USER_TO_ROLR data by role_name Data used to determine
	 * whether the role of the deletion and user association
	 * 根据角色名称查询SYS_USER_TO_ROLR表中的数据，用与判断角色被用户引用
	 * 
	 * @param id
	 * @return
	 */
	public List<UserToRoleEntity> queryUserByRoleName(String role_name) {
		List<UserToRoleEntity> userToRoleList = roleManagementMapper
				.selectSYS_USER_TO_ROLE(role_name);
		return userToRoleList;
	}

	/**
	 * Get table of SYS_ROLE_TO_RES data by role_name Data used to determine
	 * whether a deleted role is associated with a resource
	 * 根据角色名称查询SYS_ROLE_TO_RES表中的数据，用与判断角色被资源引用
	 * 
	 * @param id
	 * @return
	 */
	public List<RoleToResEntity> queryResByRoleName(String role_name) {
		List<RoleToResEntity> roleToResList = roleManagementMapper
				.selectSYS_ROLE_TO_RES(role_name);
		return roleToResList;
	}

	/**
	 * Initialization viewUserPopup.jsp by role_name 查询拥有此角色的用户
	 * 
	 * @param role_name
	 * @return
	 */
	public PageData<UserEntity> queryUserListByRoleNameListPage(QueryParam param) {
		List<UserEntity> list = roleManagementMapper
				.selectSYS_USER01ListPage(param);
		PageData<UserEntity> pd = new PageData<UserEntity>();
		pd.setQueryResult(list);
		pd.setQueryParam(param);
		return pd;
	}

	/**
	 * Query User List Except RoleName 查询不拥有此角色的用户
	 * 
	 * @param param
	 * @return
	 */
	public PageData<UserEntity> queryUserListExceptRoleNameListPage(
			QueryParam param) {
		List<UserEntity> list = roleManagementMapper
				.selectSYS_USER02ListPage(param);
		PageData<UserEntity> pd = new PageData<UserEntity>();
		pd.setQueryResult(list);
		pd.setQueryParam(param);
		return pd;
	}

	/**
	 * The role of the lifting of the relationship with the user
	 * 解除角色与用户之间的关联，即从SYS_USER_TO_ROLR删除数据
	 * 
	 * @param t
	 * @return
	 */
	public String deleteUserToRole(String unameAndRname) {
		if (unameAndRname == null) {
			return Constants.ERRMSG_PARAM_ERROR;
		}
		int count = 0;
		String[] rows = unameAndRname.split(";");
		for (int i = 0; i < rows.length; i++) {
			String[] roleanduser = rows[i].split(",");
			UserToRoleEntity userToRoleEntity = new UserToRoleEntity();
			userToRoleEntity.setUsername(roleanduser[0]);
			userToRoleEntity.setRole_name(roleanduser[1]);
			// do delete
			roleManagementMapper.deleteUserToRoleEntity01(userToRoleEntity);
			count++;
		}
		if (count == rows.length) {
			return Constants.SUCCESS_CODE;
		} else {
			return Constants.FAILURE_CODE;
		}
	}

	/**
	 * 根据用户名称解除角色与用户之间的关联
	 * 
	 * @param t
	 * @return
	 */
	public int deleteRoleByUserName(String username) {
		return roleManagementMapper.deleteUserToRoleEntity02(username);
	}

	/**
	 * add role to user 给角色添加用户
	 * 
	 * @param userToRoleEntity
	 * @return
	 */

	public String insertUserToRole(String userToRoleData) {
		if (userToRoleData == null) {
			return Constants.ERRMSG_PARAM_ERROR;
		}
		int count = 0;
		String[] rows = userToRoleData.split(";");
		for (int i = 0; i < rows.length; i++) {
			String[] roleanduser = rows[i].split(",");
			UserToRoleEntity userToRoleEntity = new UserToRoleEntity();
			userToRoleEntity.setUsername(roleanduser[0]);
			userToRoleEntity.setRole_name(roleanduser[1]);
			userToRoleEntity.setCreate_user(CommonTools.getSessionUserName());
			userToRoleEntity.setCreate_time(CommonTools.getCurrentDateTime());
			roleManagementMapper.insertSYS_USER_TO_ROLE(userToRoleEntity);
			count++;
		}
		if (count == rows.length) {
			return Constants.SUCCESS_CODE;
		} else {
			return Constants.FAILURE_CODE;
		}
	}

	/**
	 * 得到除已有角色之外的所有角色集合
	 * 
	 * @return
	 */
	public List<RoleManagementEntity> getResToRoleList(String id) {
		// 得到所有角色集合
		QueryParam param = new QueryParam();
		List<RoleManagementEntity> roleList = this.queryList(param);
		if (id != null && id != "") {
			// 通过资源ID得到该资源拥有的角色集合
			List<ResourceToRole> roleArray = resourceManagementServiceImpl
					.getRoleArray(id);
			List<String> roles = new ArrayList<String>();
			// 将对象集合转换成String集合
			for (ResourceToRole role : roleArray) {
				roles.add(role.getRoleName());
			}
			// 通过迭代器得到除已有角色之外的所有角色集合
			Iterator<RoleManagementEntity> listi = roleList.iterator();
			while (listi.hasNext()) {
				RoleManagementEntity roleEntity = listi.next();
				if (roles.contains(roleEntity.getRole_name())) {
					listi.remove();
				}
			}
		}
		return roleList;
	}

	/**
	 * Query UserRole By UserName 根据用户名获取当前用户的角色
	 * 
	 * @param userName
	 * @return
	 */
	@Override
	public List<String> getUserRoleByUserName(String userName) {
		return roleManagementMapper.getUserRoleByUserName(userName);
	}

}
